package com.jinzhuo;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/*消息订阅*/
public class topic {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setVirtualHost("/jinzhuo");
        factory.setPassword("jinzhuo");
        factory.setUsername("jinzhuo");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //创建交换机
        /**
         * exchangeDeclare(String var1, BuiltinExchangeType var2, boolean var3, boolean var4, boolean var5, Map<String, Object> var6)
         * 参数
         *  var1 交换机名称
         *  var2 交换机类型
         *         - DIRECT("direct"), 定向发送
         *         -FANOUT("fanout"), 广播（群发）
         *         -TOPIC("topic"),  通配符的方式
         *         -HEADERS("headers"); 参数匹配
         * var3 是否持久化
         * var4 是否自动删除
         * var5 是否内部使用
         * var6 参数
         */
        String exchangeName="topicExchange";
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC,true,false,false,null);

        //创建队列
        channel.queueDeclare("topicQueue1",true,false,false,null);
        channel.queueDeclare("topicQueue2",true,false,false,null);
        //绑定队列和交换机
        /**
         *  queueBind(String var1, String var2, String var3)
         *  参数
         *      -var1 队列名称
         *      -var2 交换机名称
         *      -var3 路由键 交换机为群发时设为“”
         */
        channel.queueBind("topicQueue1",exchangeName,"error.#");
        channel.queueBind("topicQueue2",exchangeName,"order.#");

        //发送消息
        String message="topic队列的发送order消息";
        channel.basicPublish(exchangeName,"order.jinzhuo",true,null,message.getBytes());

        channel.close();
        connection.close();
    }
}
